/**
 * @author ntran
 */
package  {
	import org.flixel.*;
	/**
	 * "Web"
	 * Acts as environment.
	 * Stores eggs and other items.
	 */
	public class PlayState extends FlxState{
		
		[Embed(source="data/autotiles.png")] private var tiles:Class;
		[Embed(source="data/map1.png")] private var map:Class;
		
		private var tileMap:FlxTilemap;
		private var player:Player;
		private var spiders:FlxGroup;
		
		public function PlayState() {
			super();
			
			tileMap = new FlxTilemap();
			tileMap.auto = FlxTilemap.ALT;
			tileMap.loadMap(FlxTilemap.pngToCSV(map, false, 2), tiles);
			tileMap.follow();
			add(tileMap);
			
			player = new Player(50,50);
			add(player);
			
			spiders = new FlxGroup();
			for(var i:int = 0; i < 5; i++)
			{
				add(spiders.add(new Spider(this, (FlxU.random(false)*700) + 100, (FlxU.random(false)*500) + 100)));
			}
		}
		override public function update():void
		{
			super.update();
			
			FlxU.overlap(player, spiders, onPlayerHitSpider);
			FlxU.collide(tileMap, spiders);
			FlxU.collide(player, tileMap);
		}
		
		private function onPlayerHitSpider(player:FlxObject, obj2:FlxObject):void
		{
			player.kill();
		}
	}
}